<?php

namespace App\Libs;

class GoogleAuth
{
  public static function verify($secret, $oneCode)
  {
    $ga = new \PHPGangsta_GoogleAuthenticator();
    $checkResult = $ga->verifyCode($secret, $oneCode, 5);    // 2 = 2*30sec clock tolerance
    if (!$checkResult) {
      fail('谷歌验证失败');
    }
  }

  public function getOneCodeFor($user)
  {
    $ga = new \PHPGangsta_GoogleAuthenticator();

    // if (!$user->goolge_secret) {
    //   $user->goolge_secret = $ga->createSecret();
    //   $user->save();
    // }

    return $ga->getCode($user->goolge_secret);
  }
}
